Home:ALL Converter>How to fix success problem while getting data from php to javascript file?

How to fix success problem while getting data from php to javascript file?

Ask Time:2019-05-08T13:29:22         Author:Uğur Çakaloğulları

Json Formatter

I have used before these jquery-ajax and php codes. Everything was fine but know there is a problem that success function not working. However, php codes are working, I can add data to mysql database, but I couldn't post info back to javascript file again by use "echo" or any way. Is this problem could originate because of server? I need your support.

I have checked php file is working or not and there was no problem about php. In javascript file in ajax codes, I have tried beforeSend and complete functions, everything were fine. But success function not working.

JS codes:

var userCookie = 1;
    var question_txt = document.getElementById("question_txt").value;
    var category_slct = document.getElementById("category_slct").value;

    $.ajax({
        type: "POST",
        url: websitePHP + "ask.php",
        data: {
            user : userCookie,
            quest : question_txt,
            cat : category_slct
        },

        beforeSend: function(){ 

        },
        success: function(data){
            alert(data); 
            if(data == 'ok'){
                alert('Question added');
            }
        }
    })

PHP codes:

include("ayar.php");

$userID = $_POST['user']; 
$categoryID = $_POST['cat'];
$question_txt = $_POST['quest']; 
$askedTime = time();

$addQuestion = $vt->prepare("INSERT INTO ".$QUESTIONS." (userID, categoryID, question, image, link, sight, pinned, bestAnswerID, askedTime, publishedTime, published) 
    VALUES (?,?,?,?,?,?,?,?,?,?,?)"); 
$addQuestion->execute(array(''.$userID.'',''.$categoryID.'', ''.$question_txt.'', '', '', 0, 0, '', ''.$askedTime.'', '', 0));  

echo 'ok';

exit();

I need to get back response from php to js by success function in ajax.

Thanks for your help, Best regards.

Author:Uğur Çakaloğulları,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/56034081/how-to-fix-success-problem-while-getting-data-from-php-to-javascript-file
yy